<?php

namespace addons\kzvote\controller\store;

use app\common\model\kzvote\Review as ReviewModel;
use app\common\model\kzvote\Vote as VoteModel;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Review extends BaseController
{

    /**
     * 查看
     */
    public function index()
    {
        $user = $this->auth->getUser();
        $votes = VoteModel::where('user_id', $user['id'])->where('mold','<',4)
            ->where('status','<',30)->field('id,name')->select();

        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $vote_ids = array_column((array)$votes,'id');
            $where['review.vote_id'] = ['in', $vote_ids];
            if ($params['vote_id']){
                if (!in_array($params['vote_id'], $vote_ids)){
                    $this->error('请筛选自己的活动');
                }
                $where['review.vote_id'] = $params['vote_id'];
            }
            if ($params['person_id']){
                $where['person_id'] = $params['person_id'];
            }
            if ($params['content']){
                $where['content'] = ['like',"%{$params['content']}%"];
            }
            $list = ReviewModel::alias('review')->with(['vote','person','user'])
                ->where($where)
                ->order('id desc')
                ->paginate(10);

            $result = array("total" => $list->total(), "rows" => $list->items());
            $this->success('','',$result);
        }
        return $this->view->fetch();
    }

    public function del()
    {
        $id  = $this->request->param('id');
        $model = new ReviewModel;
        if ($model->where('id','in',$id)->delete()) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
}
